package com.cda.utils;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Iterator;

/**
 * <p>
 * <Strong>Description：</Strong>rest请求工具类
 * </p>
 * 人脸识别专用httpclient
*/
public class FaceHttpClientUtil {

	protected final static Logger logger = LoggerFactory.getLogger(FaceHttpClientUtil.class);

	private static NameValuePair[] appendParam(JSONObject sendData){
		//参数设置，需要注意的就是里边不能传NULL，要传空字符串
		Iterator<String> it = sendData.keys();
		NameValuePair[] data = new NameValuePair[sendData.size()] ;
		int i=0;
		while(it.hasNext()){
			// 获得key
			String key = it.next();
			String value = sendData.getString(key);
			data[i] = new NameValuePair(key,value);
			i=i+1;
		}
		return data;
	}
	/**
	 *
	 * @param url
	 * @param sendData
	 * @return
	 * @throws IOException
	 * 发送post请求
	 */
	public static String doPostForString(final String url,
										 final JSONObject sendData) throws IOException {
		try {
			PostMethod postMethod = null;
			postMethod = new PostMethod(url) ;
			postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;

			NameValuePair[] data = appendParam( sendData);//拼接参数
			logger.info(url+" / "+JSONArray.fromObject(data));
			postMethod.setRequestBody(data);
			HttpClient httpClient = new HttpClient();
			int response = httpClient.executeMethod(postMethod); // 执行POST方法
			String result = postMethod.getResponseBodyAsString() ;
			//logger.info(result);
			return result;
		} catch (Exception e) {
			logger.error("发送post请求异常"+e.getMessage());
			throw new RuntimeException(e.getMessage());
		}
	}

	/**
	 *
	 * @param url
	 * @param sendData
	 * @return
	 * @throws IOException
	 * 发送post请求
	 */
	public static String doPostForString2(final String url,
										 final NameValuePair[] data) throws IOException {
		try {
			PostMethod postMethod = null;
			postMethod = new PostMethod(url) ;
			postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;

			postMethod.setRequestBody(data);
			HttpClient httpClient = new HttpClient();
			int response = httpClient.executeMethod(postMethod); // 执行POST方法
			String result = postMethod.getResponseBodyAsString() ;
			//logger.info(result);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("发送post请求异常"+e.getMessage());
			throw new RuntimeException(e.getMessage());
		}
	}




	/**
	 * @param url
	 * @param sendData
	 * @return
	 * @throws IOException
	 * 发送get请求
	 */
	public static String doGetForString(final String url,
										 final JSONObject sendData) throws IOException {
		try {
			GetMethod getMethod = null;
			getMethod = new GetMethod(url) ;
			getMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;

			NameValuePair[] data = appendParam( sendData);//拼接参数
			getMethod.setQueryString(data);
			HttpClient httpClient = new HttpClient();
			int response = httpClient.executeMethod(getMethod); // 执行POST方法
			String result = getMethod.getResponseBodyAsString() ;
			//logger.info(result);
			return result;
		} catch (Exception e) {
			logger.error("发送get请求异常"+e.getMessage());
			throw new RuntimeException(e.getMessage());
		}
	}
	/**
	 * @param url
	 * @param sendData
	 * @return
	 * @throws IOException
	 * 发送get请求
	 */
	public static String doGetForString1(final String url,
										final JSONObject sendData) throws IOException {
		try {
			GetMethod getMethod = null;
			getMethod = new GetMethod(url) ;
			getMethod.setRequestHeader("Content-Type", "application/json") ;

			NameValuePair[] data = appendParam( sendData);//拼接参数
			getMethod.setQueryString(data);
			HttpClient httpClient = new HttpClient();
			int response = httpClient.executeMethod(getMethod); // 执行POST方法
			String result = getMethod.getResponseBodyAsString() ;
			//logger.info(result);
			return result;
		} catch (Exception e) {
			logger.error("发送get请求异常"+e.getMessage());
			throw new RuntimeException(e.getMessage());
		}
	}

}
